package cn.itcast.web.servlet;

import cn.itcast.daomain.user;
import cn.itcast.service.UserService;
import cn.itcast.service.impl.UserServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/addUserListServlet")
public class addUserListServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");

        response.setCharacterEncoding("utf-8");
        Map<String, String[]> parameterMap = request.getParameterMap();
        user user=new user();
        try {
            BeanUtils.populate(user,parameterMap);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }

        UserService service=new UserServiceImpl();
        boolean flg= service.addUser(user);
        if (flg){
            request.getRequestDispatcher("/jihuo.jsp").forward(request,response);
        }else {
            request.setAttribute("user_name","用户已存在");
            request.getRequestDispatcher("/register.jsp").forward(request,response);
        }
    }
}
